package com.bistu.notepad_zjr;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.bistu.notepad_zjr.adapter.MyAdapter;
import com.bistu.notepad_zjr.bean.Note;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private FloatingActionButton mBtnAdd;

    private List<Note> mNotes;//数据
    private MyAdapter mMyAdapter;//适配器
    private NoteDbOpenHelper mNoteDbOpenHelper;//数据库

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();     //加载RecyclerView
        initData();     //加载数据
        initEvent();    //放数据，显示
    }

    /**
     * 刷新数据
     */
    @Override
    protected void onResume() {
        super.onResume();
        refreshDataFromDb();    //取数据
    }

    private void refreshDataFromDb() {
        mNotes = getDataFromDB();   //获取数据库中的全部数据
        mMyAdapter.refreshData(mNotes); //调用刷新方法
    }

    private void initEvent() {
        mMyAdapter = new MyAdapter(this, mNotes);
        mRecyclerView.setAdapter(mMyAdapter);   //绑定适配器
        RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);
    }

    private void initData() {
        mNotes = new ArrayList<>();
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);
    }

    //获取数据库中全部数据
    private List<Note> getDataFromDB() {
        return mNoteDbOpenHelper.queryAllFromDb();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.rlv);
    }

    //点击“+”按钮时，跳到添加页面
    public void add(View view) {
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);
    }

//    //创建菜单
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.search,menu);
//        //获取到控件
//        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
//        //监听
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            //提交时
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                return false;
//            }
//            //内容改变时
//            @Override
//            public boolean onQueryTextChange(String newText) {
//                mNotes = mNoteDbOpenHelper.queryFromDbByTitle(newText);
//                mMyAdapter.refreshData(mNotes);
//                return true;
//            }
//        });
//        return super.onCreateOptionsMenu(menu);
//    }

}